package com.laboratory.miniooperate.controller;

import com.laboratory.miniooperate.domain.UploadResult;
import com.laboratory.miniooperate.service.UploadService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@RestController
@RequestMapping("/upload")
@RequiredArgsConstructor
public class UploadController {

    private final UploadService uploadService;

    /**
     * 文件上传
     *
     * @param file
     * @return
     */
    @PostMapping(value = "/file", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public UploadResult uploadFile(@RequestParam("file") MultipartFile file) {
        return uploadService.uploadFile(file);
    }

    /**
     * 文件删除
     *
     * @param objectName
     * @return
     */
    @DeleteMapping("/file")
    public Boolean deleteFile(@RequestParam("objectName") String objectName) {
        return uploadService.deletedFile(objectName);
    }

    /**
     * 文件预览
     *
     * @param objectName
     * @return
     */
    @GetMapping("/file/view")
    public ResponseEntity<InputStreamResource> viewFile(@RequestParam("objectName") String objectName) {
        return uploadService.downloadFile(objectName, false);
    }

    /**
     * 文件下载
     *
     * @param objectName
     * @return
     */
    @GetMapping("/file/download")
    public ResponseEntity<InputStreamResource> downloadFile(@RequestParam("objectName") String objectName) {
        return uploadService.downloadFile(objectName, true);
    }

}
